package controller;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
import javax.servlet.http.*;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.io.*;
import java.nio.charset.StandardCharsets;

import service.army_service;
import service.guide_service;
import entity.*;
public class show_army_servlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private army_service am_service=new army_service();
	public show_army_servlet() {
		super();
	}
	public void destroy() {
		super.destroy(); 
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		HttpSession session= request.getSession();
		String requestURI = request.getRequestURI();
		user u=(user)session.getAttribute("user");
		String userAgent = request.getHeader("User-Agent");
		boolean is_mobile = false;
		String[] mobileKeywords = {"Android", "iPhone", "iPad", "Windows Phone"};
		for (String keyword : mobileKeywords) {
		    if (userAgent.contains(keyword)) {
		    	is_mobile = true;
		        break;
		    }
		}
		request.setAttribute("is_mobile", is_mobile);
		if (requestURI.equals("/teamfight_wiki/all_army")){
			if (u!=null && u.getType()==3){
				List<army>armys=am_service.get_army(0,"");
				request.setAttribute("armys", armys);
				request.getRequestDispatcher("/army/all_army.jsp").forward(request,response);
			}
			else{
				List<army>armys=am_service.get_army(1,"");
				request.setAttribute("armys", armys);
				request.getRequestDispatcher("/army/all_army.jsp").forward(request,response);
			}
		}
		if (requestURI.equals("/teamfight_wiki/my_army")){
			if (u!=null){
				List<army>armys=am_service.get_army(2,u.getUser_name());
				request.setAttribute("armys", armys);
				request.getRequestDispatcher("/army/all_army.jsp").forward(request,response);
			}
			else{
				request.getRequestDispatcher("/account_manage/login.jsp").forward(request,response);
			}
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
}
